const express = require("express");

const router = express.Router();

const bookmarkModel = require("../../model/bookmarkModel");

//接口1：收藏
router.post("/addbookmark", (req, res) => {
    let { user_id, news_id, news_category, news_tag, news_title, news_img, news_time, news_media, user_avators } = req.body;
    // console.log(req.body);
    bookmarkModel
        .addBookmark({ user_id, news_id, news_category, news_tag, news_title, news_img, news_time, news_media, user_avators })
        .then((data) => {
            res.send(data);
        })
        .catch((err) => {
            res.send({
                code: -1,
                msg: "收藏失败",
                err,
            });
        });
});

//接口2：取消收藏
router.post("/removebookmark", (req, res) => {
    let { user_id, news_id } = req.body;
    bookmarkModel
        .removeBookmark({ user_id, news_id })
        .then((data) => {
            res.send(data);
        })
        .catch((err) => {
            res.send({
                code: -1,
                msg: "取消收藏失败",
                err,
            });
        });
});

//接口3：获取收藏
router.get("/getbookmark", (req, res) => {
    let { user_id, news_id } = req.query;
    bookmarkModel
        .getBookmark({ user_id, news_id })
        .then((data) => {
            res.send({
                code: 200,
                msg: "获取成功",
                data,
            });
        })
        .catch((err) => {
            res.send({
                code: -1,
                msg: "获取失败",
                err,
            });
        });
});

//接口3：获取用户收藏列表
router.get("/getbookmarklist", (req, res) => {
    let { user_id } = req.query;
    bookmarkModel
        .getBookmarkList({ user_id })
        .then((data) => {
            res.send({
                code: 200,
                msg: "获取成功",
                data,
            });
        })
        .catch((err) => {
            res.send({
                code: -1,
                msg: "获取失败",
                err,
            });
        });
});

module.exports = router;
